package com.kanshu.basictest.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.util.Log;

import com.kanshu.basictest.db.BookDBHelper;


public class BookProvider extends ContentProvider {

    private static final String TAG = "BookProvider";


    public static final String AUTHORITY = "com.kanshu.basictest.book.provider";

    public static final Uri BOOK_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/book");
    public static final Uri USER_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/user");

    public static final int BOOK_URI_CODE = 0;
    public static final int USER_URI_CODE = 1;

    private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    static {
        sURIMatcher.addURI(AUTHORITY, "book", BOOK_URI_CODE);
        sURIMatcher.addURI(AUTHORITY, "user", USER_URI_CODE);
    }


    private Context mContext;

    private SQLiteDatabase mDb;


    @Override
    public boolean onCreate() {
        //onCreate()方法调用的线程:main
        Log.d(TAG, "onCreate()方法调用的线程:" + Thread.currentThread().getName());
        mContext = getContext();
        initProviderData();
        return true;
    }

    private void initProviderData() {
        mDb = new BookDBHelper(mContext).getWritableDatabase();
        mDb.execSQL("DELETE FROM " + BookDBHelper.BOOK_TABLE_NAME);
        mDb.execSQL("DELETE FROM " + BookDBHelper.USER_TABLE_NAME);

        mDb.execSQL("INSERT INTO " + BookDBHelper.BOOK_TABLE_NAME + " VALUES(3,'Android');");
        mDb.execSQL("INSERT INTO " + BookDBHelper.BOOK_TABLE_NAME + " VALUES(1,'IOS');");
        mDb.execSQL("INSERT INTO " + BookDBHelper.BOOK_TABLE_NAME + " VALUES(2,'HTML');");

        mDb.execSQL("INSERT INTO " + BookDBHelper.USER_TABLE_NAME + " VALUES(1,'李连杰',1);");
        mDb.execSQL("INSERT INTO " + BookDBHelper.USER_TABLE_NAME + " VALUES(2,'邱淑贞',0);");


    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        Log.d(TAG, "getType()方法调用的线程:" + Thread.currentThread().getName());
        return null;
    }

    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        //query()方法调用的线程:Binder_2
        Log.d(TAG, "query()方法调用的线程:" + Thread.currentThread().getName());
        String tableName = getTableName(uri);
        if (tableName == null) {
            throw new IllegalArgumentException("不支持的Uri:" + uri);
        }
        return mDb.query(tableName, projection, selection, selectionArgs, null, null, sortOrder, null);
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        Log.d(TAG, "insert()方法调用的线程:" + Thread.currentThread().getName());
        String tableName = getTableName(uri);
        if (tableName == null) {
            throw new IllegalArgumentException("不支持的Uri:" + uri);
        }
        mDb.insert(tableName, null, values);
        mContext.getContentResolver().notifyChange(uri, null);
        return uri;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        Log.d(TAG, "delete()方法调用的线程:" + Thread.currentThread().getName());
        String tableName = getTableName(uri);
        if (tableName == null) {
            throw new IllegalArgumentException("不支持的Uri:" + uri);
        }
        int count = mDb.delete(tableName, selection, selectionArgs);
        if (count > 0) {
            mContext.getContentResolver().notifyChange(uri, null);
        }
        return count;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        Log.d(TAG, "update()方法调用的线程:" + Thread.currentThread().getName());
        String tableName = getTableName(uri);
        if (tableName == null) {
            throw new IllegalArgumentException("不支持的Uri:" + uri);
        }
        int row = mDb.update(tableName, values, selection, selectionArgs);
        if (row > 0) {
            mContext.getContentResolver().notifyChange(uri, null);
        }
        return row;
    }

    private String getTableName(Uri uri) {
        String tableName = null;
        switch (sURIMatcher.match(uri)) {
            case BOOK_URI_CODE:
                tableName = BookDBHelper.BOOK_TABLE_NAME;
            case USER_URI_CODE:
                tableName = BookDBHelper.BOOK_TABLE_NAME;
            default:
                break;
        }
        return tableName;
    }


}
